package org.linphone.sub.send;

import com.xuhao.android.libsocket.sdk.bean.ISendable;

import org.linphone.sub.utils.CRCUtil;
import org.linphone.sub.utils.SocketUtil;

import java.util.ArrayList;

/**
 * Created by Xiang on 2018/4/12.
 */

public class SendBroadData implements ISendable {

	//准备发送的数据数组
	public byte[] bb;
	//根据与服务端的协议封装数据
	public SendBroadData(byte[] bc, ArrayList<String> al){
		if(al.size()!=0){
			bb = new byte[6+4*al.size()+bc.length+1];
			String[] s;

			bb[0] = (byte) 0xfb;
			bb[1] = (byte) 0xd2;
			bb[2] = SocketUtil.int2bytes(6+4*al.size()+bc.length+3)[0];
			bb[3] = SocketUtil.int2bytes(6+4*al.size()+bc.length+3)[1];
			bb[4] = 0x00;
			bb[5] = (byte)al.size();

			for(int i=0;i<al.size();i++){
				s = al.get(i).split("[.]");
				bb[6+4*i] = (byte)Integer.parseInt(s[0]);
				bb[7+4*i] = (byte)Integer.parseInt(s[1]);
				bb[8+4*i] = (byte)Integer.parseInt(s[2]);
				bb[9+4*i] = (byte)Integer.parseInt(s[3]);
			}
			for(int x=0;x<bc.length;x++){
				bb[6+4*al.size()+x] = bc[x];
			}
			bb[6+4*al.size()+bc.length] = (byte) 0xfd;

		}else {
			bb = new byte[7+bc.length+1];
			bb[0] = (byte) 0xfb;
			bb[1] = (byte) 0xd2;
			bb[2] = SocketUtil.int2bytes(7+4*al.size()+bc.length+3)[0];
			bb[3] = SocketUtil.int2bytes(7+4*al.size()+bc.length+3)[1];
			bb[4] = 0x00;
			bb[5] = (byte)al.size();
			bb[6] = 0x00;
			for(int x=0;x<bc.length;x++){
				bb[7+x] = bc[x];
			}
		  bb[7+bc.length] = (byte) 0xfd;

		}



	}
	//格式化数据为可发送的数据格式
	@Override
	public byte[] parse() {
		return CRCUtil.toCrc16(bb);
	}
}
